﻿
goog.provide('gaillard.RestaurantFormFragment');

goog.require('goog.dom');
goog.require('goog.ui.Dialog');
goog.require('gaillard.Model');

/**
 *
 * @constructor
 * @extends {goog.ui.Dialog}
 */
gaillard.RestaurantFormFragment = function(model) {
    goog.ui.Dialog.call(this, 'restaurant-dialog', false);
    
    this.setModel(model);
    this.setDraggable(false);
    
    var custButtonSet = new goog.ui.Dialog.ButtonSet().
                            addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.SAVE, true, false).
                            addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.CANCEL, false, true);
    this.setButtonSet(custButtonSet);
    
    var handler = this.getHandler();
    handler.listen(this, goog.ui.Dialog.EventType.SELECT, this.submitInsert_);
    handler.listen(model, gaillard.Model.Datatype.OPEN_INSERT_RESTAURANT, this.openRestaurantFormChanged_);
};
goog.inherits(gaillard.RestaurantFormFragment, goog.ui.Dialog);

gaillard.RestaurantFormFragment.template = '<form id="restaurant-form" action=""><table><tbody><tr><td class="name"><span class="field_name">Nom</span></td><td class="value"><input id="restaurant-form-name" type="text" size="50"></td></tr><tr><td class="name"><span class="field_name">Adresse</span></td><td class="value"><input id="restaurant-form-address" type="text" size="50"></td></tr><tr><td class="name"><span class="field_name">Téléphone</span></td><td class="value"><input id="restaurant-form-phone" type="text" size="50"></td></tr><tr><td class="name"><span class="field_name">Lien</span><span class="field_type">(http://...)</span></td><td class="value"><input id="restaurant-form-link"type="text" size="50"></td></tr><tr><td class="name"><span class="field_name">Description</span></td><td class="value"><textarea id="restaurant-form-description" rows="5" cols="50"></textarea></td></tr></tbody></table></form>';

gaillard.RestaurantFormFragment.prototype.openRestaurantFormChanged_ = function () {
    var open = this.getModel().get(gaillard.Model.Datatype.OPEN_INSERT_RESTAURANT);
    if (open) {

        var position = this.getModel().get(gaillard.Model.Datatype.SELECTED_RESTAURANT);
        if (position > -1) {
            
        }
    }
    
    this.setContent(gaillard.RestaurantFormFragment.template);
    this.setVisible(open);
};

gaillard.RestaurantFormFragment.prototype.submitInsert_ = function (event) {
    if (event.key == goog.ui.Dialog.DefaultButtonKeys.SAVE) {
        this.getModel().insertRestaurant([
            goog.dom.getElement('restaurant-form-name').value,
            goog.dom.getElement('restaurant-form-address').value,
            goog.dom.getElement('restaurant-form-phone').value,
            goog.dom.getElement('restaurant-form-link').value,
            goog.dom.getElement('restaurant-form-description').value,
            48,
            2
        ]);
    }
};